<template>
  <div>
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <!-- <span>添加用户</span> -->
      <el-form ref="form" :model="addForm" label-width="70px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitform">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { adduser } from '../api/index.js'
export default {
  data() {
    const validateEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else {
        const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-])+/
        if (reg.test(value)) {
          // this.$refs.ruleForm2.validateField('checkPass')
          callback()
        } else {
          callback(new Error('邮箱输入不正确'))
        }
      }
    }
    const validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else {
        const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (reg.test(value)) {
          // this.$refs.ruleForm2.validateField('checkPass')
          callback()
        } else {
          callback(new Error('手机号输入不正确'))
        }
      }
    }
    return {
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        email: [{ required: true, validator: validateEmail, trigger: 'blur' }],
        mobile: [{ required: true, validator: validatePhone, trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapState({
      dialogVisible: state => state.Users.dialogVisible,
      addForm: state => state.Users.addForm
    })
  },
  methods: {
    ...mapMutations(['changedialogVisible', 'getusermenu']),
    closeDialog() {
      this.changedialogVisible(false)
      this.$refs.form.resetFields()
    },
    submitform() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // alert('submit!')
          this.addusers()
          // this.changedialogVisible(false)
          // this.$refs.form.resetFields()
        } else {
          this.$message.error('表单格式不正确')
          return false
        }
      })
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.changedialogVisible(false)
          this.$refs.form.resetFields()
        })
        .catch(_ => {})
    },
    async addusers() {
      const { data: res } = await adduser(this.addForm)
      // console.log(res)
      if (res.meta.status !== 201) {
        this.changedialogVisible(true)
        this.$message.error(res.meta.msg)
      } else {
        this.changedialogVisible(false)
        this.$refs.form.resetFields()
        this.$message.success(res.meta.msg)
        this.getusermenu()
      }
    }
  }
}
</script>

<style></style>
